package com.ruicar.afs.cloud.apply.pre.loan.mq.sender.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.apply.common.entity.*;
import com.ruicar.afs.cloud.apply.contract.enums.AffiliatedUnitEnum;
import com.ruicar.afs.cloud.apply.contract.service.*;
import com.ruicar.afs.cloud.apply.contract.service.impl.ApplyContractInfoServiceImpl;
import com.ruicar.afs.cloud.apply.contract.vo.ContractEntryCollectVO;
import com.ruicar.afs.cloud.apply.pre.loan.condition.CaseSubmitInfoCondition;
import com.ruicar.afs.cloud.apply.pre.loan.condition.ContractCancelCondition;
import com.ruicar.afs.cloud.apply.pre.loan.service.ApplyAffiliatedUnitService;
import com.ruicar.afs.cloud.apply.pre.loan.service.ApplyCarDetailsService;
import com.ruicar.afs.cloud.common.core.constant.CommonConstants;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.LoanProcessTypeEnum;
import com.ruicar.afs.cloud.common.modules.dto.mq.loan.*;
import com.ruicar.afs.cloud.common.modules.dto.mq.loan.gpsdto.ApplyGpsInfoDto;
import com.ruicar.afs.cloud.common.modules.enums.OprTypeEnum;
import com.ruicar.afs.cloud.common.mq.rabbit.message.AfsTransEntity;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import com.ruicar.afs.cloud.image.entity.ComAttachmentFile;
import com.ruicar.afs.cloud.image.service.ComAttachmentFileService;
import com.ruicar.afs.cloud.parameter.commom.enums.InsuranceTypeEnums;
import com.ruicar.afs.cloud.parameter.commom.enums.WhetherEnum;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 进件放款提交案件审批实现类
 *
 * @author panhj
 */
@Service
@Slf4j
@AllArgsConstructor
public class CaseLoanInfoSenderImpl {
    /***
     * GPS申请service
     */
    private final ApplyGpsInfoService applyGpsInfoService;
    /**
     * 融资项目service
     */
    private final ApplyAddPriceItemsService applyAddPriceItemsService;
    /**
     * 保险申请service
     */
    private final ApplyInsuranceInfoService applyInsuranceInfoService;
    /**
     * 车辆发票service
     */
    private final ApplyCarInvoiceService applyCarInvoiceService;

    private final ApplyCustHistoryService applyCustHistoryService;
    /**
     * 银行卡申请service
     */
    private final ApplyBankCardService applyBankCardService;
    /**
     * 挂靠service
     */
    private final ApplyAffiliatedUnitService applyAffiliatedUnitService;
    /***
     *合同申请service
     */
    private final ApplyContractInfoService applyContractInfoService;
    /**
     * 车辆申请service
     */
    private final ApplyCarDetailsService applyCarDetailsService;
    private final ApplyContractInfoServiceImpl applyContractInfoServiceImpl;
    private final ComAttachmentFileService comAttachmentFileService;
    private final GpsProductInfoService gpsProductInfoService;
    /**
     * @param condition
     * @return
     */
    public AfsTransEntity<LoanApplySubmitInfo> returnLoanApplySubmitInfo(CaseSubmitInfoCondition condition) {
        LoanApplySubmitInfo loanApplySubmitInfo = new LoanApplySubmitInfo();
        AfsTransEntity<LoanApplySubmitInfo> transEntity = new AfsTransEntity<>();
        ArrayList<AttachmentDTO> attachmentDtoList = new ArrayList<>();
        List<ComAttachmentFile> comAttachmentFileList = this.comAttachmentFileService.list(Wrappers.<ComAttachmentFile>query().lambda()
                .eq(StringUtil.isNotEmpty(condition.getContractNo()), ComAttachmentFile::getBusiNo, condition.getContractNo()));
        if (CollectionUtil.isNotEmpty(comAttachmentFileList)) {
            comAttachmentFileList.forEach(comAttachmentFile -> {
                AttachmentDTO attachmentDTO = new AttachmentDTO();
                BeanUtil.copyProperties(comAttachmentFile, attachmentDTO);
                attachmentDtoList.add(attachmentDTO);
            });
        }
        //合同信息
        CaseContractInfoDTO caseContractInfoDto = this.caseContractInfoDTO(condition.getContractNo());
        caseContractInfoDto.setAttachmentDtoList(attachmentDtoList);
        loanApplySubmitInfo.setApplyNo(condition.getApplyNo());
        loanApplySubmitInfo.setCaseContractInfoDto(caseContractInfoDto);
        loanApplySubmitInfo.setContractNo(condition.getContractNo());
        loanApplySubmitInfo.setOprType(OprTypeEnum.OPRADD.getCode());
        loanApplySubmitInfo.setUserRealName(condition.getUserRelName());
        loanApplySubmitInfo.setPhone(condition.getPhone());
        loanApplySubmitInfo.setProcessType(LoanProcessTypeEnum.GENERAL_LOAN.getCode());
        log.info("*******数据******" + loanApplySubmitInfo);
        transEntity.setTransCode(MqTransCode.AFS_POS_APPLY_CASE_CTM_NEW_LOAN);
        transEntity.setData(loanApplySubmitInfo);
        return transEntity;

    }
    public AfsTransEntity<ApplyGpsInfoDto> returngpsInfoSubmitInfo(ContractEntryCollectVO vo) {
        AfsTransEntity<ApplyGpsInfoDto> transEntity = new AfsTransEntity<>();
        //二次派单
        ApplyGpsInfoDto applyGpsInfoDto = this.applyGpsInfoDto(vo.getGpsInfo().getApplyNo());
        log.info("*******GPS二次派单数据******" + applyGpsInfoDto);
        transEntity.setTransCode(MqTransCode.AFS_POS_APPLY_CASE_CTM_GPS_AGAIN_DISTRIBUTE);
        transEntity.setData(applyGpsInfoDto);
        return transEntity;
    }
    public AfsTransEntity<LoanDiscardDTO> contractCancel( ContractCancelCondition condition) {
        AfsTransEntity<LoanDiscardDTO> transEntity = new AfsTransEntity<>();
        //合同取消
        LoanDiscardDTO loanDiscardDTO = new LoanDiscardDTO();
        loanDiscardDTO.setContractNo(condition.getContractNo());
        loanDiscardDTO.setOperationType(condition.getCancelOrWithdraw());
        loanDiscardDTO.setStatus(condition.getContractApplyStatus());
        loanDiscardDTO.setApplyNo(condition.getApplyNo());
        log.info("*******合同取消/撤回******" + loanDiscardDTO);
        transEntity.setTransCode(MqTransCode.AFS_POS_APPLY_CASE_CTM_DISCARD_LOAN);
        transEntity.setData(loanDiscardDTO);
        return transEntity;
    }

    /**
     * 放款需要的融资信息list
     *
     * @param carId
     * @return
     */
    public ArrayList<AddedFinancingItemsDTO> addedFinancingItemsInfo(Long carId) {
        List<ApplyAddPriceItems> applyAddPriceItemsList = this.applyAddPriceItemsService.list(Wrappers.<ApplyAddPriceItems>query().lambda()
                .eq(ApplyAddPriceItems::getCarId, String.valueOf(carId)));
        ArrayList<AddedFinancingItemsDTO> addedFinancingItemsDtoArrayList = new ArrayList<>();
        if (applyAddPriceItemsList != null) {
            for (ApplyAddPriceItems applyAddPriceItems : applyAddPriceItemsList) {
                AddedFinancingItemsDTO addedFinancingItemsDto = new AddedFinancingItemsDTO();
                addedFinancingItemsDto.setApplyNo(applyAddPriceItems.getApplyNo());
                addedFinancingItemsDto.setFinanceItemAmt(applyAddPriceItems.getAddFinanceAmt());
                addedFinancingItemsDto.setFinanceItemCode(applyAddPriceItems.getFinanceItemCode() == null ? "" : applyAddPriceItems.getFinanceItemCode());
                addedFinancingItemsDto.setFinanceItemName(applyAddPriceItems.getFinanceItemName() == null ? "" : applyAddPriceItems.getFinanceItemName());
                addedFinancingItemsDto.setIsCalLoanAmt("");
                if (StringUtil.isNotEmpty(applyAddPriceItems.getCarId())) {
                    addedFinancingItemsDto.setCarId(Long.valueOf(applyAddPriceItems.getCarId()));
                }
                addedFinancingItemsDto.setId(applyAddPriceItems.getId());
                addedFinancingItemsDto.setCreateBy(applyAddPriceItems.getCreateBy());
                addedFinancingItemsDto.setCreateTime(applyAddPriceItems.getCreateTime());
                addedFinancingItemsDto.setUpdateBy(applyAddPriceItems.getUpdateBy());
                addedFinancingItemsDto.setUpdateTime(applyAddPriceItems.getUpdateTime());
                addedFinancingItemsDto.setDelFlag(applyAddPriceItems.getDelFlag());
                addedFinancingItemsDto.setParentItemId(applyAddPriceItems.getUpperId());
                addedFinancingItemsDtoArrayList.add(addedFinancingItemsDto);
            }
        }
        return addedFinancingItemsDtoArrayList;
    }

    /**
     * 放款需要的GPS信息list
     */
    public CarGpsApplyDTO carGpsApplyInfo(Long carId) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ApplyGpsInfo applyGpsInfo = this.applyGpsInfoService.getOne(Wrappers.<ApplyGpsInfo>query().lambda()
                .eq(ApplyGpsInfo::getCarId, String.valueOf(carId)));
        CarGpsApplyDTO carGpsApplyDTO = new CarGpsApplyDTO();
        GpsProductInfo gpsProductInfo=gpsProductInfoService.getOne(Wrappers.<GpsProductInfo>query().lambda()
                .eq(GpsProductInfo::getGpsProductId,applyGpsInfo.getGpsProductNo())
                .eq(GpsProductInfo::getGpsFirm,applyGpsInfo.getGpsSupplier()));
        if(ObjectUtil.isNotNull(gpsProductInfo)){
            carGpsApplyDTO.setGpsFullName(gpsProductInfo.getGpsFullName());
        }
        if (ObjectUtil.isNotNull(applyGpsInfo)) {
            carGpsApplyDTO.setApplyNo(applyGpsInfo.getApplyNo());
            carGpsApplyDTO.setGpsSupplier(applyGpsInfo.getGpsSupplier() == null ? "" : applyGpsInfo.getGpsSupplier());
            carGpsApplyDTO.setIsRobberyBag(applyGpsInfo.getIsRobberyBag() == null ? "" : applyGpsInfo.getIsRobberyBag());
            carGpsApplyDTO.setGpsProductNo(applyGpsInfo.getGpsProductNo() == null ? "" : applyGpsInfo.getGpsProductNo());
            carGpsApplyDTO.setPreInstallTime(applyGpsInfo.getPreInstallTime() == null ? "" : simpleDateFormat.format(applyGpsInfo.getPreInstallTime()));
            carGpsApplyDTO.setLoanTerm(applyGpsInfo.getLoanTerm());
            carGpsApplyDTO.setSiteContact(applyGpsInfo.getSiteContact() == null ? "" : applyGpsInfo.getSiteContact());
            carGpsApplyDTO.setContactPhone(applyGpsInfo.getContactPhone() == null ? "" : applyGpsInfo.getContactPhone());
            carGpsApplyDTO.setInstallProvince(applyGpsInfo.getInstallProvince() == null ? "" : applyGpsInfo.getInstallProvince());
            carGpsApplyDTO.setInstallCity(applyGpsInfo.getInstallCity() == null ? "" : applyGpsInfo.getInstallCity());
            carGpsApplyDTO.setInstallCounty(applyGpsInfo.getInstallCounty() == null ? "" : applyGpsInfo.getInstallCounty());
            carGpsApplyDTO.setInstallStreet(applyGpsInfo.getInstallStreet() == null ? "" : applyGpsInfo.getInstallStreet());
            carGpsApplyDTO.setInstallAddress(applyGpsInfo.getInstallAddress() == null ? "" : applyGpsInfo.getInstallAddress());
            carGpsApplyDTO.setApplyStatus(applyGpsInfo.getApplyStatus() == null ? "" : applyGpsInfo.getApplyStatus());
            carGpsApplyDTO.setOrderNo(applyGpsInfo.getOrderNo() == null ? "" : applyGpsInfo.getOrderNo());
            carGpsApplyDTO.setRemarks(applyGpsInfo.getRemarks() == null ? "" : applyGpsInfo.getRemarks());
            carGpsApplyDTO.setGpsType(applyGpsInfo.getGpsType()==null?"":applyGpsInfo.getGpsType());
        }
        return carGpsApplyDTO;
    }

    /**
     * 放款需要的车辆保险信息list
     */
    public ArrayList<CarInsuranceInfoDTO> carInsuranceInfo(Long carId, String contractNo) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        ArrayList<CarInsuranceInfoDTO> carInsuranceInfoDtoArrayList = new ArrayList<>();
        List<ApplyInsuranceInfo> applyInsuranceInfoList = this.applyInsuranceInfoService.list(Wrappers.<ApplyInsuranceInfo>query().lambda()
                .eq(ApplyInsuranceInfo::getCarId, String.valueOf(carId)));
        ApplyContractInfo applyContractInfo = this.applyContractInfoService.getOne(Wrappers.<ApplyContractInfo>query().lambda()
                .eq(ApplyContractInfo::getContractNo, contractNo));
        if (applyInsuranceInfoList != null && applyContractInfo != null) {
            for (ApplyInsuranceInfo applyInsuranceInfo : applyInsuranceInfoList) {
                CarInsuranceInfoDTO carInsuranceInfoDto = new CarInsuranceInfoDTO();
                carInsuranceInfoDto.setApplyNo(applyContractInfo.getApplyNo());
                carInsuranceInfoDto.setContractNo(applyContractInfo.getContractNo() == null ? "" : applyContractInfo.getContractNo());
                carInsuranceInfoDto.setInsuranceMode(applyInsuranceInfo.getInsuranceMode() == null ? "" : applyInsuranceInfo.getInsuranceMode());
                carInsuranceInfoDto.setCarVin(applyInsuranceInfo.getCarVin() == null ? "" : applyInsuranceInfo.getCarVin());
                //电子保单使用性质需调第三方接口
                carInsuranceInfoDto.setPurpose("");
                carInsuranceInfoDto.setIsEffective("");
                carInsuranceInfoDto.setInsuranceCompany(applyInsuranceInfo.getInsuranceCompany() == null ? "" : applyInsuranceInfo.getInsuranceCompany());
                carInsuranceInfoDto.setInsuranceNo(applyInsuranceInfo.getInsuranceNo() == null ? "" : applyInsuranceInfo.getInsuranceNo());
                if (applyInsuranceInfo.getInsuranceType() != null) {
                    if ("business".equals(applyInsuranceInfo.getInsuranceType())) {
                        carInsuranceInfoDto.setInsuranceType(InsuranceTypeEnums.BUSINESS.getCode());
                    } else if ("compulsory".equals(applyInsuranceInfo.getInsuranceType())) {
                        carInsuranceInfoDto.setInsuranceType(InsuranceTypeEnums.COMPULSORY.getCode());
                    }
                } else {
                    carInsuranceInfoDto.setInsuranceType("");
                }
                carInsuranceInfoDto.setInsuranceAmt(applyInsuranceInfo.getInsuranceAmt() == null ? 0 : applyInsuranceInfo.getInsuranceAmt().doubleValue());
                carInsuranceInfoDto.setInsuranceStartTime(applyInsuranceInfo.getInsuranceStartTime() == null ? "" : simpleDateFormat.format(applyInsuranceInfo.getInsuranceStartTime()));
                carInsuranceInfoDto.setInsuranceEndTime(applyInsuranceInfo.getInsuranceEndTime() == null ? "" : simpleDateFormat.format(applyInsuranceInfo.getInsuranceEndTime()));
                carInsuranceInfoDtoArrayList.add(carInsuranceInfoDto);
            }

        }
        return carInsuranceInfoDtoArrayList;
    }

    /**
     * 放款需要的车辆发票信息list
     */
    public CarInvoiceInfoDTO carInvoiceInfo(Long carId, String contractNo) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        CarInvoiceInfoDTO carInvoiceInfoDto = new CarInvoiceInfoDTO();
        ApplyCarInvoice applyCarInvoice = this.applyCarInvoiceService.getOne(Wrappers.<ApplyCarInvoice>query().lambda()
                .eq(ApplyCarInvoice::getCarId, String.valueOf(carId)));
        ApplyContractInfo applyContractInfo = this.applyContractInfoService.getOne(Wrappers.<ApplyContractInfo>query().lambda()
                .eq(ApplyContractInfo::getContractNo, contractNo));
        ApplyCarDetails applyCarDetails = this.applyCarDetailsService.getOne(Wrappers.<ApplyCarDetails>query().lambda()
                .eq(ApplyCarDetails::getId, carId));
        carInvoiceInfoDto.setApplyNo(applyCarDetails.getApplyNo());
        if (applyContractInfo != null) {
            carInvoiceInfoDto.setContractNo(applyContractInfo.getContractNo() == null ? "" : applyContractInfo.getContractNo());
        }
        if (applyCarInvoice != null) {
            carInvoiceInfoDto.setInvoiceCode(applyCarInvoice.getInvoiceCode() == null ? "" : applyCarInvoice.getInvoiceCode());
            carInvoiceInfoDto.setInvoiceNumber(applyCarInvoice.getInvoiceNo() == null ? "" : applyCarInvoice.getInvoiceNo());
            carInvoiceInfoDto.setInvoiceAmt(applyCarInvoice.getInvoiceAmt());
            carInvoiceInfoDto.setVerifyNo("");
            carInvoiceInfoDto.setInvoiceMinAmt(applyCarInvoice.getInvoiceAmt());
            carInvoiceInfoDto.setInvoiceDate(applyCarInvoice.getInvoiceDate() == null ? "" : simpleDateFormat.format(applyCarInvoice.getInvoiceDate()));
            carInvoiceInfoDto.setCheckResult("");
            carInvoiceInfoDto.setInvoiceRate(applyCarInvoice.getInvoiceRate());
            carInvoiceInfoDto.setMakeInvoiceUnit(applyCarInvoice.getInvoiceUnit() == null ? "" : applyCarInvoice.getInvoiceUnit());
            carInvoiceInfoDto.setTaxNumber(applyCarInvoice.getTaxpayerNo() == null ? "" : applyCarInvoice.getTaxpayerNo());
        }
        if (applyCarDetails != null) {
            carInvoiceInfoDto.setEngineNo(applyCarDetails.getEngineNo() == null ? "" : applyCarDetails.getEngineNo());
            carInvoiceInfoDto.setFullModelNum(applyCarDetails.getFullModelNum() == null ? "" : applyCarDetails.getFullModelNum());
            carInvoiceInfoDto.setCarVin(applyCarDetails.getCarVin() == null ? "" : applyCarDetails.getCarVin());
        }
        return carInvoiceInfoDto;
    }

    /**
     * 放款需要的客户证件信息list
     */
    public ArrayList<CertificateInfoDTO> certificateInfo(String applyNo) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<ApplyCustHistory> applyCustBaseInfoList = this.applyCustHistoryService.list(Wrappers.<ApplyCustHistory>query().lambda()
                .eq(ApplyCustHistory::getApplyNo, applyNo));
        ArrayList<CertificateInfoDTO> certificateInfoList = new ArrayList<>();
        if (applyCustBaseInfoList != null) {
            for (ApplyCustHistory applyCustHistory : applyCustBaseInfoList) {
                CertificateInfoDTO certificateInfoDto = new CertificateInfoDTO();
                certificateInfoDto.setApplyNo(applyNo);
                certificateInfoDto.setCustRole(applyCustHistory.getCustRole() == null ? "" : applyCustHistory.getCustRole());
                certificateInfoDto.setCertNo(applyCustHistory.getCertNo() == null ? "" : applyCustHistory.getCertNo());
                certificateInfoDto.setCustName(applyCustHistory.getCustName() == null ? "" : applyCustHistory.getCustName());
                certificateInfoDto.setCertType(applyCustHistory.getCertType() == null ? "" : applyCustHistory.getCertType());
                //时间暂时写死，引入ocr后修改
                if (applyCustHistory.getIsLongTerm()) {
                    certificateInfoDto.setCertEndDate("2099-12-31");
                    certificateInfoDto.setIsLongTerm(WhetherEnum.YES.getIndex());
                } else {
                    certificateInfoDto.setCertEndDate(applyCustHistory.getCertEndDate() == null ? "" : simpleDateFormat.format(applyCustHistory.getCertEndDate()));
                    certificateInfoDto.setIsLongTerm(WhetherEnum.No.getIndex());
                }
                certificateInfoList.add(certificateInfoDto);
            }
        }
        return certificateInfoList;
    }

    /**
     * 放款需要的银行信息list
     */
    public LoanBankCardInfoDTO loanBankCardInfo(String contractNo) {
        ApplyBankCard applyBankCard = this.applyBankCardService.getOne(Wrappers.<ApplyBankCard>query().lambda()
                .eq(ApplyBankCard::getContractNo, contractNo));
        ApplyContractInfo applyContractInfo = this.applyContractInfoService.getOne(Wrappers.<ApplyContractInfo>query().lambda()
                .eq(ApplyContractInfo::getContractNo, contractNo));
        LoanBankCardInfoDTO loanBankCardInfoDto = new LoanBankCardInfoDTO();
        if (ObjectUtil.isNotNull(applyContractInfo)) {
            loanBankCardInfoDto.setApplyNo(applyBankCard.getApplyNo());
            loanBankCardInfoDto.setContractNo(applyContractInfo.getContractNo() == null ? "" : applyContractInfo.getContractNo());
            loanBankCardInfoDto.setBankCode(applyBankCard.getBankCode() == null ? "" : applyBankCard.getBankCode());
            loanBankCardInfoDto.setBankBranch(applyBankCard.getBankBranch() == null ? "" : applyBankCard.getBankBranch());
            loanBankCardInfoDto.setAccountName(applyBankCard.getAccountName() == null ? "" : applyBankCard.getAccountName());
            loanBankCardInfoDto.setAccountNo(applyBankCard.getAccountNo() == null ? "" : applyBankCard.getAccountNo());
            loanBankCardInfoDto.setCertNo(applyBankCard.getCertNo() == null ? "" : applyBankCard.getCertNo());
            loanBankCardInfoDto.setBankPhone(applyBankCard.getBankPhone() == null ? "" : applyBankCard.getBankPhone());
            loanBankCardInfoDto.setVerCode(applyBankCard.getVerCode() == null ? "" : applyBankCard.getVerCode());
            loanBankCardInfoDto.setVerStatus(applyBankCard.getVerStatus() == null ? "" : applyBankCard.getVerStatus());
            loanBankCardInfoDto.setAuthorizeWay(applyBankCard.getAuthorizeWay() == null ? "" : applyBankCard.getAuthorizeWay());
            loanBankCardInfoDto.setOpenProvince(applyBankCard.getOpenProvince()==null?"":applyBankCard.getOpenProvince());
            loanBankCardInfoDto.setOpenCity(applyBankCard.getOpenCity()==null?"":applyBankCard.getOpenCity());
            loanBankCardInfoDto.setOpenAddress(applyBankCard.getOpenAddress()==null?"":applyBankCard.getOpenAddress());
        }

        return loanBankCardInfoDto;
    }

    /**
     * 放款需要的挂靠信息list
     */
    public PersonalAffiliatedUnitsDTO personalAffiliatedUnits(String applyNo) {
        PersonalAffiliatedUnitsDTO personalAffiliatedUnitsDto = new PersonalAffiliatedUnitsDTO();
        ApplyAffiliatedUnit applyAffiliatedUnit = this.applyAffiliatedUnitService.getOne(Wrappers.<ApplyAffiliatedUnit>query().lambda()
                .eq(ApplyAffiliatedUnit::getApplyNo, applyNo));
        personalAffiliatedUnitsDto.setApplyNo(applyNo);
        if (applyAffiliatedUnit != null) {
            personalAffiliatedUnitsDto.setAffiliatedName(applyAffiliatedUnit.getAffiliatedName() == null ? "" : applyAffiliatedUnit.getAffiliatedName());
            personalAffiliatedUnitsDto.setSocUniCrtCode(applyAffiliatedUnit.getSocUniCrtCode() == null ? "" : applyAffiliatedUnit.getSocUniCrtCode());
            personalAffiliatedUnitsDto.setAffiliatedType(applyAffiliatedUnit.getAffiliatedType() == null ? "" : applyAffiliatedUnit.getAffiliatedType());
        }
        return personalAffiliatedUnitsDto;
    }

    /**
     * 放款需要的合同信息list
     *
     * @param contractNo
     * @return
     */
    public CaseContractInfoDTO caseContractInfoDTO(String contractNo) {
        ApplyContractInfo caseContractInfoDTOS = this.applyContractInfoService.getOne(Wrappers.<ApplyContractInfo>query().lambda()
                .eq(ApplyContractInfo::getContractNo, contractNo));
        CaseContractInfoDTO caseContractInfoDTO = new CaseContractInfoDTO();
        if (ObjectUtil.isNotNull(caseContractInfoDTOS)) {
            caseContractInfoDTO.setApplyNo(caseContractInfoDTOS.getApplyNo());
            caseContractInfoDTO.setContractNo(caseContractInfoDTOS.getContractNo() == null ? "" : caseContractInfoDTOS.getContractNo());
            caseContractInfoDTO.setCreateBy(caseContractInfoDTOS.getCreateBy() == null ? "" : caseContractInfoDTOS.getCreateBy());
            caseContractInfoDTO.setGpsStatus(caseContractInfoDTOS.getGpsStatus() == null ? "" : caseContractInfoDTOS.getGpsStatus());
            caseContractInfoDTO.setWitnessId(caseContractInfoDTOS.getWitnessIdCard());
            caseContractInfoDTO.setLendingMode(caseContractInfoDTOS.getLendingMode());
            List<ApplyCarDetails> carDetailsList = this.applyCarDetailsService.list(Wrappers.<ApplyCarDetails>query().lambda().
                    eq(StringUtil.isNotEmpty(caseContractInfoDTOS.getContractNo()), ApplyCarDetails::getContractNo, caseContractInfoDTOS.getContractNo()));
            ArrayList<CarInfoDTO> carInfoDtoList = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(carDetailsList)) {
                for (ApplyCarDetails applyCarDetails : carDetailsList) {
                    CarInfoDTO carInfoDTO = new CarInfoDTO();
                    ApplyAffiliatedUnit applyAffiliatedUnit = this.applyAffiliatedUnitService.getOne(Wrappers.<ApplyAffiliatedUnit>query().lambda()
                            .eq(ApplyAffiliatedUnit::getApplyNo, applyCarDetails.getApplyNo()));
                    //PersonalAffiliatedUnitsDTO personalAffiliatedUnitsDTO = this.personalAffiliatedUnits(applyCarDetails.getApplyNo());
                    if (ObjectUtil.isNotNull(applyAffiliatedUnit)) {
                        if (AffiliatedUnitEnum.person.getCode().equals(applyAffiliatedUnit.getAffiliatedType())) {
                            carInfoDTO.setAffiliatedName(applyAffiliatedUnit.getAffiliatedName() == null ? "" : applyAffiliatedUnit.getAffiliatedName());
                            carInfoDTO.setSocUniCrtCode(applyAffiliatedUnit.getSocUniCrtCode() == null ? "" : applyAffiliatedUnit.getSocUniCrtCode());
                        }
                    }
                    carInfoDTO.setApplyNo(applyCarDetails.getApplyNo() == null ? "" : applyCarDetails.getApplyNo());
                    carInfoDTO.setContractNo(applyCarDetails.getContractNo() == null ? "" : applyCarDetails.getContractNo());
                    carInfoDTO.setCarId(applyCarDetails.getId());
                    carInfoDTO.setCarColor(applyCarDetails.getCarColor() == null ? "" : applyCarDetails.getCarColor());
                    carInfoDTO.setCarVin(applyCarDetails.getCarVin() == null ? "" : applyCarDetails.getCarVin());
                    carInfoDTO.setLicensePlate(applyCarDetails.getLicensePlate() == null ? "" : applyCarDetails.getLicensePlate());
                    carInfoDTO.setEngineNo(applyCarDetails.getEngineNo() == null ? "" : applyCarDetails.getEngineNo());
                    carInfoDTO.setFullModelNum(applyCarDetails.getFullModelNum() == null ? "" : applyCarDetails.getFullModelNum());
                    CarGpsApplyDTO carGpsApplyDTO = this.carGpsApplyInfo(applyCarDetails.getId());
                    carInfoDTO.setCarGpsApplyDto(carGpsApplyDTO);
                    CarInvoiceInfoDTO carInvoiceInfoDTO = this.carInvoiceInfo(applyCarDetails.getId(), applyCarDetails.getContractNo());
                    carInfoDTO.setCarInvoiceInfoDTO(carInvoiceInfoDTO);
                    ArrayList<AddedFinancingItemsDTO> addedFinancingItemsDTOS = this.addedFinancingItemsInfo(applyCarDetails.getId());
                    carInfoDTO.setAddedFinancingItemsDTOList(addedFinancingItemsDTOS);
                    ArrayList<CarInsuranceInfoDTO> carInsuranceInfoDTOS = this.carInsuranceInfo(applyCarDetails.getId(), applyCarDetails.getContractNo());
                    carInfoDTO.setCarInsuranceInfoDtoList(carInsuranceInfoDTOS);
                    carInfoDtoList.add(carInfoDTO);
                }
            }
            caseContractInfoDTO.setCarInfoDtoList(carInfoDtoList);
            ArrayList<CertificateInfoDTO> certificateInfoDTOS = this.certificateInfo(caseContractInfoDTOS.getApplyNo());
            ArrayList<CaseCustHistoryDTO> caseCustHistoryDTOS=this.custHistoryInfo(caseContractInfoDTO.getApplyNo());
            caseContractInfoDTO.setCaseCustHistoryDTOList(caseCustHistoryDTOS);
            caseContractInfoDTO.setCertificateInfoDTOList(certificateInfoDTOS);
            LoanBankCardInfoDTO loanBankCardInfoDTOS = this.loanBankCardInfo(caseContractInfoDTOS.getContractNo());
            caseContractInfoDTO.setLoanBankCardInfoDto(loanBankCardInfoDTOS);
        }
        return caseContractInfoDTO;
    }

    public ArrayList<CaseCustHistoryDTO> custHistoryInfo(String applyNo) {
        List<ApplyCustHistory> applyCustBaseInfoList = this.applyCustHistoryService.list(Wrappers.<ApplyCustHistory>query().lambda()
                .eq(ApplyCustHistory::getApplyNo, applyNo));
        ArrayList<CaseCustHistoryDTO> caseCustHistoryDTOS = new ArrayList<>();
        if (applyCustBaseInfoList != null) {
            for (ApplyCustHistory applyCustHistory : applyCustBaseInfoList) {
                CaseCustHistoryDTO caseCustHistoryDTO=new CaseCustHistoryDTO();
                BeanUtils.copyProperties(applyCustHistory, caseCustHistoryDTO);

                caseCustHistoryDTOS.add(caseCustHistoryDTO);
            }
        }
        return caseCustHistoryDTOS;
    }
    /**
     * 二次派单信息
     *
     * @param applyNo
     * @return
     */
    public ApplyGpsInfoDto applyGpsInfoDto(String applyNo) {
        ApplyGpsInfoDto applyGpsInfoDto=new ApplyGpsInfoDto();
        ApplyGpsInfo applyGpsInfo=applyGpsInfoService.getOne(Wrappers.<ApplyGpsInfo>query().lambda().eq(ApplyGpsInfo::getApplyNo,applyNo));
        GpsProductInfo gpsProductInfo=gpsProductInfoService.getOne(Wrappers.<GpsProductInfo>query().lambda()
                .eq(GpsProductInfo::getGpsProductId,applyGpsInfo.getGpsProductNo())
                .eq(GpsProductInfo::getGpsFirm,applyGpsInfo.getGpsSupplier()));
        applyGpsInfo.setCreateBy(SecurityUtils.getUser().getUserRealName());
        BeanUtils.copyProperties(applyGpsInfo, applyGpsInfoDto);
        applyGpsInfoDto.setGpsFullName(gpsProductInfo.getGpsFullName());
        return applyGpsInfoDto;
    }
}
